package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.advanced;


import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.socks.server.AbstractHandleSocksReportServerProxyClientResponseTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.constant.ProxyMessageType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.socks.handler.NettySocksClientProxyServerRealHandler;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.core.utils.ObjectUtils;


@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleSocksReportServerProxyClientResponseTypeAdvanced
        extends AbstractHandleSocksReportServerProxyClientResponseTypeAdvanced<NettyProxyMsg> {


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     * @see NettySocksClientProxyServerRealHandler
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {

        Channel channel = nettyChannelContext.channel();
        Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
        if(ObjectUtils.isNotEmpty(nextChannel)&&nextChannel.isActive()){
            ByteBuf buf = nextChannel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
            buf.writeBytes(nettyProxyMsg.getData());
            nextChannel.writeAndFlush(buf);
        }else {
            log.error("服务端代理客户端socks，本地通道已关闭！");
        }
    }

}
